package com.zb.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.entity.SelectEmpVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjWorkClientStrategy;
import com.zb.entity.ZjWorkClientStrategyCondition;
import com.zb.order.service.ConWorkClientStrategyService;
import com.zb.result.PageUtil;
import com.zb.service.ZjWorkClientStrategyConditionService;
import com.zb.service.ZjWorkClientStrategyEmpService;
import com.zb.service.ZjWorkClientStrategyService;
import com.zb.vo.WorkClientStrategyListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author WEN
 * @Date 2023/9/27 0027
 **/
@Service
public class ConWorkClientStrategyServiceImpl implements ConWorkClientStrategyService {
    @Resource
    private ZjWorkClientStrategyService workClientStrategyService;
    @Resource
    private ZjWorkClientStrategyConditionService workClientStrategyConditionService;
    @Resource
    private ZjWorkClientStrategyEmpService workClientStrategyEmpService;

    // 获取工单客服分配规则列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjWorkClientStrategy> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "weight", "type", "emp_type", "is_disabled AS disabled", "gmt_create", "gmt_update");
        wrapper.eq("cust_acc_id", custAccId);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjWorkClientStrategy> page = workClientStrategyService.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<ZjWorkClientStrategyCondition> conditions = workClientStrategyConditionService.getParentConditionByWorkClientStrategyIds(page.getRecords().stream().map(ZjWorkClientStrategy::getId).toList());
        List<Long> serviceEmpIds = page.getRecords().stream().filter(s -> 1 == s.getEmpType()).map(ZjWorkClientStrategy::getId).toList();
        List<Long> serviceGroupIds = page.getRecords().stream().filter(s -> 2 == s.getEmpType()).map(ZjWorkClientStrategy::getId).toList();
        List<SelectEmpVo> serviceEmps = workClientStrategyEmpService.getServiceEmpByWorkClientStrategyIds(serviceEmpIds);
        List<SelectEmpVo> serviceGroups = workClientStrategyEmpService.getServiceGroupByWorkClientStrategyIds(serviceGroupIds);

        List<WorkClientStrategyListVo> voList = page.getRecords().stream().map(client -> {
            WorkClientStrategyListVo vo = new WorkClientStrategyListVo();
            BeanUtils.copyProperties(client, vo);
            vo.setStatus(client.getDisabled());
            vo.setCreateTime(client.getGmtCreate());
            vo.setUpdateTime(client.getGmtUpdate());
            vo.setCondition(conditions.stream().filter(con -> Objects.equals(con.getWorkClientStrategyId(), vo.getId())).map(c -> {
                return 1 == c.getType()? "品牌" : 2 == c.getType()? "商品分类" : 3 == c.getType()? "客服区域" : "工单模块";
            }).collect(Collectors.joining("、")));
            if (1 == client.getEmpType()) {
                vo.setEmp(serviceEmps.stream().filter(s -> Objects.equals(s.getId(), client.getId())).map(SelectEmpVo::getName).collect(Collectors.joining("、")));
            }
            if (2 == client.getEmpType()) {
                vo.setEmp(serviceGroups.stream().filter(s -> Objects.equals(s.getId(), client.getId())).map(SelectEmpVo::getName).collect(Collectors.joining("、")));
            }
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
